<?php
namespace app\model;

use think\Model;
use think\facade\Db;

/**
 * 自提点模型
 */
class PickupModel extends Model
{
    protected $pk = 'id';
    // 设置当前模型对应的完整数据表名称
    protected $table = 'sh_pick_up';

    /**
     * 获取自提点列表（带分页）
     * @param array $filter 筛选条件
     * @return array
     */
    public static function get_list($filter = [])
    {
        $uniacid = current_uni_account();
        
        $where_arr = [];
        $where_arr[] = ' uniacid=' . $uniacid;
        
        // 名称筛选
        if (!empty($filter['name'])) {
            $where_arr[] = ' name like "%' . $filter['name'] . '%"';
        }
        
        // 状态筛选
        if (isset($filter['status'])) {
            $where_arr[] = ' status=' . intval($filter['status']);
        }
        
        // 地址筛选
        if (!empty($filter['address'])) {
            $where_arr[] = ' (province like "%' . $filter['address'] . '%" '
                . ' or city like "%' . $filter['address'] . '%" '
                . ' or area like "%' . $filter['address'] . '%" '
                . ' or detail like "%' . $filter['address'] . '%")';
        }
        
        $where_str = '';
        if (!empty($where_arr)) {
            $where_str = ' where ' . join(' and ', $where_arr);
        }
        
        $sql = 'select * from sh_pick_up' . $where_str;
        $sql .= ' order by id desc';
        
        $total_sql = 'select count(id) as total from sh_pick_up' . $where_str;
        
        $page_size = isset($_GET['page_size']) ? intval($_GET['page_size']) : 15;
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;
        $sql .= ' limit ' . ($page - 1) * $page_size . ',' . $page_size;
        
        return [
            'list' => Db::query($sql),
            'page' => [
                'total' => Db::query($total_sql)[0]['total'],
                'page_size' => $page_size,
                'page' => $page,
            ]
        ];
    }

    /**
     * 获取单个自提点信息
     * @param int $pickup_id 自提点ID
     * @return array|null
     */
    public static function single($pickup_id)
    {
        $sql = 'select * from sh_pick_up where id=' . intval($pickup_id);
        $result = Db::query($sql);
        
        return !empty($result) ? $result[0] : null;
    }

    /**
     * 编辑自提点
     * @param int $pickup_id 自提点ID
     * @param array $data 数据
     * @return int
     */
    public static function edit($pickup_id, $data)
    {
        $sql = 'update sh_pick_up set '
            . 'name="' . $data['name'] . '"'
            . ',phone="' . $data['phone'] . '"'
            . ',province="' . $data['province'] . '"'
            . ',city="' . $data['city'] . '"'
            . ',area="' . $data['area'] . '"'
            . ',street="' . $data['street'] . '"'
            . ',detail="' . $data['detail'] . '"'
            . ',latitude="' . $data['latitude'] . '"'
            . ',longitude="' . $data['longitude'] . '"'
            . ',status=' . intval($data['status'])
            . ' where id=' . intval($pickup_id);

        return Db::execute($sql);
    }

    /**
     * 新增自提点
     * @param array $data 数据
     * @return string|int 新增ID
     */
    public static function add($data)
    {
        $uniacid = current_uni_account();
        
        $insertData = [
            'uniacid' => $uniacid,
            'name' => $data['name'],
            'phone' => $data['phone'],
            'province' => $data['province'],
            'city' => $data['city'],
            'area' => $data['area'],
            'street' => $data['street'],
            'detail' => $data['detail'],
            'latitude' => $data['latitude'] ?? '',
            'longitude' => $data['longitude'] ?? '',
            'status' => $data['status'] ?? 1,
        ];

        return Db::name('sh_pick_up')->insertGetId($insertData);
    }

    /**
     * 删除自提点
     * @param int $pickup_id 自提点ID
     * @return int
     */
    public static function remove($pickup_id)
    {
        $sql = 'delete from sh_pick_up where id=' . intval($pickup_id);

        return Db::execute($sql);
    }
}

